package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000138696423";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCK8SvAXNpFBM7ooxHdtniVlc9FYQwoCVPbXB+1toMnc8jFNH5XCBUUidAkoDDqBR7xkKHdycw8O50XpEBC2s9gWHbH3u6dk5p3x0v+FCGxfwb9P2K5Ux6zGWsWTZ8NK7GdOtaKKN4ziqEwuBL2hj92QzWGIPyYi3iHPIyiM++eU5v7LGt+LK+pC+R+80nX2QEqwMA6jgdZ8E68mXoPed4S/Az5sNF04pXxblX5cHsNrJn9So8K4SmALIrBWV4ZsXdEqQQ9NHhDzn1sBL+hLz+SBYuaf7hKfoGne+xKMIr/Fey3w99fQpTpGMhwOxgZL6DnC05BDkzK0+r4xIUy6ZfVAgMBAAECggEBAIXtD9Ic7lum1dZSEQ+YKkVtoNvsPq58xzLjasFaCrPFnFnSTpGPfdpNOK+FJYD9rKUGV7AUKuRIRhUawsJubYQd4WipD3Iea79XAH4JY6zWl6MWLRAb+bSDJMDb0xr76fl3VILZ/8zIrLTsSYMNRjC1eesu8qwLjAM3LwvOagM+gi3oCt2St8ao7Ri3IwJOgEJ4SRhEXY80/7UlgRcPA9V3DJlXkOkmUUcQF+MJqL+JpFqpnNLugOP9n/ZzHoNeYm3IzOf+BoF8RvpmGO+u6YAGTBN322VhCYnQ2XBV90eUjokM2XIns7VueOjnRtX5fvdVHrsbx5GR20BfiwHVB2ECgYEAx2753CovbddmQA1J4wCRXlQcnHL0w8Pux2mveS7ISY9xQfcvr+VLSkvCbTnyYqz4sIhxk1V6ZssH7Wpggs5tgh1QIjy7ROk2op0c4cSokns7uYig8NjdMqapSeH4iW7PFUCx0ohOCFDivdD9vFWRbaNZBaephJnbe6fDq5hog/0CgYEAslndWVrAItDanQy1ikD9t2Fx5xyT3NxuleiAvCQfZzJpZ327YGVc9D7ZpVN/4RKU0ETao4QcD8vQi7xDiT3ANTb+MX8S1jBmRk9cgATM5JaRfZOHre2s1pmnIkhigpzs2whsesFiHOlRI53EkzFvY+D7PYfpyDTB/5isJ8px7rkCgYB9jc05oUlIwqG502PfyJy8IQH+MJUNpg1ud4ekKCKpxOeNmvvnpdg3huh5cVC6dKEAKnYuRlMP4eQ0wJLP5vukPaYUV92h7Zya1YS4UeG6jmC0VmLUHgNr/uZ3AA9DHoK6o4cPvwY5UArfa3JCnjA3MO47lCkxtNcryxtxrTqnVQKBgFVGQF5j1LAQ2iXzGK3U6zqN/e75q/0S7FECzOKoBtfJs7oG1Nt/O5WQo3G33dHYl4yJD1zVYrFuyvS0/DmXa9pFw/4YewIsPApsEZcoNGsrTr8TFfFkTZqB9vaeU3ON4nXLrkp3Qhj+uYvk+ENEhLTlslq2LGJMT2GNNusDkbwJAoGBAKSJpeIVvFJeXyOQBVHukMr4sEWjntBULzdCD0abiu3fmDQCoPQarbvzdxTDfOG3o2f+i/h8cpnwbjn67Ubu43+m1eiO4xRf4gIZbGBtApOYxUJxMXyJFnN47b9aX7p+ueLKXZZYv0hhuL44GxXQr6VEytNsSsvSX5nImcc+jRps";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAur4JpyvhvlxOuPabjIA2xfhwGnsXdP94CIkzAi2cro7WIG/3sFMffN7BFnTYlfzhGdNaT4iHU2svc1j0bnmqcZm+Ro3jXB/1Xq4wBqN+Jvk++U0igObU/Gd5kgGp+NWHqxg6D7Db+hX7p+Cpi9jLu62CRML4mf6varsszLQR8s9pMTHQucxljm6tro7aATzS3UDrP7NCi+t9ZigRN2lnwL8qksvf5yEp+hMQcSDKjL+NmwwFN+fo4prC5BJrVpC5FgUcQcVZe+4pH6NGl/oKZ6anz1x0lujLImJFjlQK6iakBrkeR7dha2A5VD0Ivc9FDilPj9LBFIpezQ31i8J9vwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                //加入超时时间是为了防止订单已经关闭了，但还支付成功了
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
